package com.right.handler;

import com.right.factory.NodeStatus;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;

/**********************************************
 * @author pillar
 * @date 2018/6/13
 * 连接状态监听
 **********************************************/
@Slf4j
public class ChannelEventHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent e = (IdleStateEvent) evt;
            if (e.state() == IdleState.READER_IDLE || e.state() == IdleState.ALL_IDLE) {
              log.info("连接空闲中");
            }
        }
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        super.channelInactive(ctx);
        String lockNo = NodeStatus.getInstance().getLockNo(ctx.channel());
        log.error("客户端关闭={}"+lockNo);
        if (lockNo != null){
            //修改数据库锁状态为离线
        }
    }
}
